package com.example.admin.service.imp;

import com.example.admin.service.WorkStremService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricVariableInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class ImpWorkStrem implements WorkStremService {
    @Autowired
    TaskService taskService;
    @Autowired
    RuntimeService runtimeService;
    @Autowired
    HistoryService historyService;
    @Override
    public void startprocess(String processDefinitionKey, String bussinessId, Map<String, Object> variables) {
        //启动工作流
        ProcessInstance Instance = runtimeService.startProcessInstanceByKey(processDefinitionKey, bussinessId, variables);
        //查询当前业务下的process
        List<Task> tasks = taskService.createTaskQuery().processInstanceBusinessKey(bussinessId).list();

        for (Task task : tasks) {
            if ("0".equals(task.getFormKey())){
                taskService.complete(task.getId());
            }
        }
    }

    @Override
    public void complete(String taskId, String msg,Integer op) {
        HashMap<String, Object> maps = new HashMap<>();
        maps.put("msg",msg);
        if (null != op){
            maps.put("op",op);
        }
        taskService.complete(taskId,maps);
    }

    @Override
    public Long findguwen(String taskId,String variableName) {
        HistoricVariableInstance historicVariableInstance = historyService.createHistoricVariableInstanceQuery().taskId(taskId).variableName(variableName).singleResult();
        Object value = historicVariableInstance.getValue();
        long id = Long.parseLong(value.toString());
        return id;
    }

}
